<?php	namespace Home\Controller;
class CategoryController extends CommonController
{
	public function index()
	{
		$cid = Q('get.cid',0,'intval');
//		p($cid);
		//获取分类ID的所有商品
		$CateSon = new \Common\Model\Cate;
		//获取类型ID
		$lid = $CateSon->where('cid',$cid)->pluck('oftype_lid');
		//静态变量
		static $cids = array();
		//后去子级的cid
		$CateSonData = $CateSon ->where('cfid',$cid) -> get();
		foreach ($CateSonData as $k => $v) {
			$cids[] = $v['cid'];
		}
		//获取的子级数组转换为字符串
		$cids = implode(',', $cids);
//		p($CateSonData);
		//判断数组是否存在
		if($CateSonData){
			//存在的话获取子级的数据
			$CateSonData = $CateSon->where("cid in($cids)")->get();
			//找子集
		}else{
			$CateSonData =array();
		}
			View::with('$CateSonData',$CateSonData);
			
			$cidson = $this->getSon($cid);
			$cidson = implode(',', $cidson);
			//在商品表中通过参类id查找出商品数据
			$gids = Db::table("goods")->where("cate_cid in ($cidson)")->lists("sid");
//			p($gids);
		if($gids){
				$CategoodsData = Db::table('typeattr')->where('typeattr_lid',$lid)->get();
			//将里面的字符转为数组
			foreach ($CategoodsData as $k => $v) {
//				$CategoodsData[$k]['xvalue'] = explode(',', $v['xvalue']);
				$CategoodsData[$k]['xvalue'] = Db::table('goodsattr')->where('attribute_xid',$v['xid'])->groupBy("shopvalue")->get();
			}
//			p($CategoodsData);
			//分配数据
			View::with('CategoodsData',$CategoodsData);
			//进行筛选
			//param=283_0_0_0_0_0_0_0_0_0
			//设置param参数，用0填充
			$param = isset($_GET['param']) ? explode('_', $_GET['param']) : array_fill(0, count($CategoodsData), 0);
//			p($param);
			View::with('param',$param);
			foreach ($param as $k => $v) {
				//没有筛选的时候不处理
				if($v)
				{
					$filterGids = Db::table('goodsattr ga1')->join('goodsattr ga2','ga2.shopvalue','=','ga1.shopvalue')->where('ga1.oid',$v)->get();
				}
			}
//			p($filterGids);
				if(isset($filterGids))
				{
					foreach ($filterGids as $k => $v) {
						$odlfilterGids[0][]=$v['goods_sid'];
					}
				}
//					p($odlfilterGids);
//					p($olddata);
			if(isset($odlfilterGids))
			{
				//求交集
				$finalGids = $odlfilterGids[0];
				foreach ($odlfilterGids as $ke => $va) 
				{
//					p($finalGids);
//					p($va);
					$afinalGids = array_intersect($finalGids,$va);
//					p($afinalGids);
				}
//				p($afinalGids);
				//第二次交集可以过滤不同分类的
				$finalGids = array_intersect($finalGids,$gids);
				/*p($gids);
				p($finalGids);*/
			}else{
				$finalGids = $gids;
			}
		}else{
			$finalGids = array();
		}
//		p($finalGids);
		if($finalGids){
//			p($finalGids);
				$gids = implode(',', $finalGids);
//				p($gids);
				$goodsshop = Db::table('goods')->where("sid in($gids)")->get();
//				p($goodsshop);
				View::with('goodsshop',$goodsshop);
		}
			
		View::with('CateSonData',$CateSonData);
//		p($CateSonData);
		//获取商品的所有数据，条件为，cid
		$CateData = Db::table('goods')->where('cate_cid',$cid)->get();
//		p($CateData);
		//获取分类所有数据
		$FatherData = $this->getFather(Db::table('cate')->get(),$cid);
//		p($FatherData);
		$FatherData = array_reverse($FatherData);
		View::with('FatherData',$FatherData);
		View::make();
	}
	/*
	 * 找父级
	 */
	public function getFather($Data,$cid)
	{
		static $tmp=array();
		foreach ($Data as $k => $v) {
			if($v['cid']==$cid){
				$tmp[] = $v;
				$this->getFather($Data, $v['cfid']);
			}
		}
		return $tmp;
	}
/**
	 * 把自己追加到自己的子集当中
	 */
	public function getSon($cid)
	{
		$cids = $this->getSonCid(Db::table("cate")->get(),$cid);
		$cids[] = $cid;
		return $cids;
	}
	/**
	 * 找子集
	 */
	public function getSonCid($data,$cid)
	{
		static $temp = array();
		foreach ($data as $k => $v) {
			if($cid==$v['cfid'])
			{
				$temp[] = $v['cid'];
				$this->getSon($data,$v['cid']);
			}
		}
		return $temp;
	}
}
